package com.blog4j.user.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.blog4j.common.annotation.OperationLog;
import com.blog4j.common.enums.ModuleTypeEnum;
import com.blog4j.common.enums.OperationTypeEnum;
import com.blog4j.common.model.Result;
import com.blog4j.requestRate.annotation.RequestRate;
import com.blog4j.user.service.FansService;
import com.blog4j.user.vo.req.FansListReqVo;
import com.blog4j.user.vo.req.FollowReqVo;
import com.blog4j.user.vo.resp.FansRespVo;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author 98k灬
 * @version v1.0.0
 * @Description : 功能描述
 * @Create on : 2024/8/1 13:35
 **/
@RequestMapping("/fans")
@RestController
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class FansController {
    private final FansService fansService;

    /**
     * 根据用户ID获取用户的粉丝信息列表
     *
     * @param reqVo 请求信息
     * @return 用户的粉丝信息列表
     */
    @RequestRate
    @OperationLog(module= ModuleTypeEnum.USER, description = "根据用户ID获取用户的粉丝信息列表")
    @PostMapping("/list")
    public Result list(@RequestBody @Valid FansListReqVo reqVo) {
        PageInfo<FansRespVo> pageInfo = fansService.pageList(reqVo);
        return Result.ok(pageInfo);
    }

    /**
     * 检查userid2是否是userid1的粉丝
     *
     * @param userId1 用户1
     * @param userId2 用户2
     * @return 是否
     */
    @RequestRate
    @OperationLog(module= ModuleTypeEnum.USER, description = "检查userid2是否是userid1的粉丝")
    @SaCheckLogin
    @GetMapping("/checkIsFans")
    public Result checkIsFans(@RequestParam("userId1") String userId1, @RequestParam("userId2") String userId2) {
        boolean flag = fansService.checkIsFans(userId1, userId2);
        return Result.ok(flag);
    }

    /**
     * 用户关注或取消关注
     *
     * @param reqVo 请求
     * @return 结果
     */
    @RequestRate(count = 5)
    @OperationLog(operationType= OperationTypeEnum.EDIT,
            module= ModuleTypeEnum.USER,
            description = "用户关注或取消关注")
    @SaCheckLogin
    @PostMapping("/follow")
    public Result follow(@RequestBody @Valid FollowReqVo reqVo) {
        fansService.follow(reqVo);
        return Result.ok();
    }
}
